﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Reflection;
using System.ComponentModel;

namespace music_control
{
    public class ViewModel : INotifyPropertyChanged
    {
        public static bool isChanged = false;

        private Controller _Controller;
        public Controller Controller
        {
            get
            {
                if (_Controller == null)
                    _Controller = new Controller();

                return _Controller;
            }
        }

        private bool _autostart;
        public bool autostart
        {
            get
            {
                return Autostart.IsAutoStartEnabled("music-control",Assembly.GetExecutingAssembly().Location);
            }

            set
            {
                this._autostart = value;

                if (_autostart == true)
                {
                    Autostart.SetAutoStart("music-control", Assembly.GetExecutingAssembly().Location);
                }
                else
                {
                    Autostart.UnSetAutoStart("music-control");
                }

                RaisePropertyChanged("autostart");
            }
        }


        public void StartAllPlugins()
        {
            Controller.StartAllPlugin();
        }

        #region OpenOptionsCommand

        RelayCommand _openOptionCommand;
        public ICommand OpenOptionCommand
        {
            get
            {
                if (_openOptionCommand == null)
                {
                    _openOptionCommand = new RelayCommand(param => this.OpenOptionsExecute(),param => this.OpenOptionsCanExecute());
                }
                return _openOptionCommand;
            }
        }

        public bool OpenOptionsCanExecute()
        {
            return true;
        }

        public void OpenOptionsExecute()
        {
            Options op = new Options(this);
            op.Show();
        }

        #endregion

        #region SaveOptionsCommand

        RelayCommand _saveOptionCommand;
        public ICommand SaveOptionCommand
        {
            get
            {
                if (_saveOptionCommand == null)
                {
                    _saveOptionCommand = new RelayCommand(param => this.SaveOptionsExecute(), param => this.SaveOptionsCanExecute());
                }
                return _saveOptionCommand;
            }
        }

        public bool SaveOptionsCanExecute()
        {
            return true;
        }

        public void SaveOptionsExecute()
        {
            _Controller.SaveSettings();

            isChanged = false;
        }

        #endregion

        #region ResetOptionsCommand

        RelayCommand _resetOptionCommand;
        public ICommand resetOptionCommand
        {
            get
            {
                if (_resetOptionCommand == null)
                {
                    _resetOptionCommand = new RelayCommand(param => this.ResetOptionsExecute(), param => this.ResetOptionsCanExecute());
                }
                return _resetOptionCommand;
            }
        }

        public bool ResetOptionsCanExecute()
        {
            return true;
        }

        public void ResetOptionsExecute()
        {
            _Controller.ResetSettings();
            RaisePropertyChanged("Controller");
        }

        #endregion

        #region CloseApplicationCommand

        RelayCommand _closeAppCommand;
        public ICommand CloseAppCommand
        {
            get
            {
                if (_closeAppCommand == null)
                {
                    _closeAppCommand = new RelayCommand(param => this.closeAppExecute(), param => this.closeAppCanExecute());
                }
                return _closeAppCommand;
            }
        }

        public bool closeAppCanExecute()
        {
            return true;
        }

        public void closeAppExecute()
        {
            Controller.Close();

            System.Environment.Exit(0);
        }

        #endregion


        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }    
    }
}
